package com.renwei.yunlong.activity.news;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.SelectGroupAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.GroupChooseBean;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.SimpleOptionView;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GroupChooseActivity extends BaseActivity implements SelectGroupAdapter.SelectGroupListenter {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private SelectGroupAdapter adapter;
    public static HashMap<String, String> groupMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_choose);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        groupMap = (HashMap<String, String>) getIntent().getSerializableExtra("groupMap");
        initView();
        initData();
    }

    private void initView() {
        simpleTileView.setTitle("选择职能");
        simpleTileView.setRightText("提交", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("groupMap", groupMap);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SelectGroupAdapter(mContext);
        adapter.setListenter(this);
        recyclerView.setAdapter(adapter);
    }


    /**
     * 添加群组选中的群组
     *
     * @param groupId
     * @param groupName
     */
    private void addGroup(String groupId, String groupName) {
        if (groupMap != null) {

            if (StringUtils.value(groupMap.get(StringUtils.value(groupId))).length() == 0) {
                if (groupMap.size()<3) {
                    groupMap.put(StringUtils.value(groupId), StringUtils.value(groupName));
                } else {
                    showCenterInfoMsg("最多只能选择三个职能");
                }
            } else {
                groupMap.remove(StringUtils.value(groupId));
            }

        } else {
            groupMap = new HashMap<>();
            groupMap.put(StringUtils.value(groupId), StringUtils.value(groupName));
        }
        adapter.notifyDataSetChanged();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
        } else if ("2".equals(companyType)) {
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
        }
        ServiceRequestManager.getManager().searchFunctionGroup(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        GroupChooseBean bean = new Gson().fromJson(result, GroupChooseBean.class);
        if (bean.getMessage().getCode() == 200) {
            adapter.refresh(bean.getRows());
        } else {
            showCenterInfoMsg(bean.getMessage().getMessage());
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }

    /**
     * 打开activity
     *
     * @param context
     * @param requeCode
     * @param groupMap
     */
    public static void openActivity(Activity context, int requeCode, HashMap<String, String> groupMap) {
        Intent intent = new Intent(context, GroupChooseActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("groupMap", groupMap);
        intent.putExtras(bundle);
        context.startActivityForResult(intent, requeCode);
    }


    /**
     * 职能选择
     *
     * @param id
     * @param name
     */
    @Override
    public void groupSelect(String id, String name) {
        addGroup(id, name);
    }
}
